/*
 * Created by Jaren at 2021/10/29 15:55
 */
import 'dart:typed_data';

import 'package:binary/binary.dart';
import 'package:inmotion_protocol/common/compact.dart';
import 'package:inmotion_protocol/common/product_model.dart';
import 'package:inmotion_protocol/common/product_version.dart';
import 'package:inmotion_protocol/lorin/bean/scooter/scooter_settings.dart';
import 'package:inmotion_protocol/util/byte_util.dart';

class S1Settings extends ScooterSettings {

    static const int lightEffectModeBothOff = 0; // 全关
    static const int lightEffectModeLightEffectOn = 1; // 氛围灯常亮(自动转向关)
    static const int lightEffectModeBothOn = 2; // 氛围灯常亮且自动转向开
    static const int lightEffectModeAutoTurnSignalOn = 3; // 自动转向灯开(氛围灯关)

    Uint4 lightEffectMode = Uint4.zero; // 底部氛围灯样式

    Uint4 smartAccForceState = Uint4.zero; // 加速力度智能调节
    Uint4 hdcState = Uint4.zero; // 下坡辅助
    Uint4 hacState = Uint4.zero; // 上坡辅助

    S1Settings(ProductModel model, VersionWrapper version) : super(model, version);

  @override
    void initCompact() {
        compact = DefaultS1Settings();
    }

    /// 自动转向灯是否开启
    bool isAutoTurnSignalOn() {
        return _isAutoTurnSignalOn(lightEffectMode.value);
    }

    /// 氛围灯是否开启
    bool isLightEffectOn() {
        return _isLightEffectOn(lightEffectMode.value);
    }

    /// 根据当前氛围灯模式[mode]和下个状态[lightEffectOnOff]或[autoTurnSignal]计算最终mode
    static int calLightEffectMode(int mode, {bool ?lightEffectOnOff, bool ?autoTurnSignal}) {
        if (lightEffectOnOff != null) {
            // 开关氛围灯
            if (_isAutoTurnSignalOn(mode)) {
                return lightEffectOnOff ? lightEffectModeBothOn : lightEffectModeAutoTurnSignalOn;
            }
            return lightEffectOnOff ? lightEffectModeLightEffectOn : lightEffectModeBothOff;
        }
        if (autoTurnSignal != null) {
            // 开关自动转向灯
            if (_isLightEffectOn(mode)) {
                return autoTurnSignal ? lightEffectModeBothOn : lightEffectModeLightEffectOn;
            }
            return autoTurnSignal ? lightEffectModeAutoTurnSignalOn : lightEffectModeBothOff;
        }
        return mode;
    }

    static bool _isAutoTurnSignalOn(int mode) {
        return mode == lightEffectModeBothOn || mode == lightEffectModeAutoTurnSignalOn;
    }

    static bool _isLightEffectOn(int mode) {
        return mode == lightEffectModeBothOn || mode == lightEffectModeLightEffectOn;
    }
}

class DefaultS1Settings extends Compact<S1Settings> {
    @override
    void fromBytes(S1Settings output, Uint8ClampedList bytes, int offset) {
        output.zeroStartupState = ByteUtil.bitValue(bytes[offset], 0, 1);
        output.cruiseState = ByteUtil.bitValue(bytes[offset], 1, 1);
        output.speedUnit = ByteUtil.bitValue(bytes[offset], 2, 1);
        output.ridingMode = output.toRidingMode(ByteUtil
            .bitValue(bytes[offset], 3, 3)
            .value);
        output.lockMode = ByteUtil.bitValue(bytes[offset], 6, 1);
        // 剩余1bit保留
        offset++;
        output.lightEffectMode = ByteUtil.bitValue(bytes[offset], 0, 2);
        output.smartAccForceState = ByteUtil.bitValue(bytes[offset], 2, 1);
        output.safeSpeedLimitState = ByteUtil.bitValue(bytes[offset], 3, 1);
        output.hdcState = ByteUtil.bitValue(bytes[offset], 4, 1);
        output.hacState = ByteUtil.bitValue(bytes[offset], 5, 1);
        // 剩余2bit保留
        offset++;
        output.accelerateForce = ByteUtil.bytes2Uint8Le(bytes, offset);
        offset++;
        output.brakeForce = ByteUtil.bytes2Uint8Le(bytes, offset);
        offset++;
        output.limitSpeed = ByteUtil.bytes2Uint16Le(bytes, offset);
    }

    @override
    int length() {
        return 6;
    }
}
